package com.tiger.pulldata;

import com.tiger.pulldata.openAccount.OpenAccountService;
import jakarta.annotation.PostConstruct;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;

@Service
public class Task {
    private final OpenAccountService openAccountService;

    public Task(OpenAccountService openAccountService) {
        this.openAccountService = openAccountService;
    }

    @PostConstruct
    public void initOpenAccount() throws IOException { //初始化拉取老师，学生，部门
        openAccountService.getApiJwtToken();
        openAccountService.pullDepts();
        openAccountService.pullTeacher(null);
        openAccountService.pullStudent(null);

        openAccountService.onPullTeacherTask(); //监听拉取老师的任务
        openAccountService.onPullStudentTask(); //监听拉取学生的任务
    }

    /**
     * 每半小时更新一次token
     * **/
    @Scheduled(cron = "0 0/30 * * * ?")
    public void updateOpenAccountToken() {
        openAccountService.getApiJwtToken();
    }

    /***
     * 每天凌晨去拉取
     * **/
    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨0点0分0秒执行
    public void taskToRunEveryDayAtMidnight() throws IOException {
        System.out.println("任务执行时间拉取: " + LocalDateTime.now());
        openAccountService.pullDepts();
        openAccountService.pullTeacher(null);
        openAccountService.pullStudent(null);
    }

}
